package org.lee.obs.event;

import org.lee.obs.LotteryResult;
import org.lee.obs.event.listener.EventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * EventManager
 * 事件管理
 *
 * @author dralee
 * @date 2021-11-23 18:53:56
 **/
public class EventManager {

    Map<EventType, List<EventListener>> listeners = new HashMap<>();

    public EventManager(EventType... operations) {
        for (EventType operation : operations) {
            listeners.put(operation, new ArrayList<>());
        }
    }

    public enum EventType {
        MESSAGE(0, "短信"),
        MQ(1, "MQ消息"),
        ;
        private int code;
        private String name;

        EventType(int code, String name) {
            this.code = code;
            this.name = name;
        }
    }

    /**
     * 订阅
     *
     * @param eventType: 事件类型
     * @param listener:  监听
     * @return void
     * @author dralee
     * @date 2021/11/23 18:59
     **/
    public void subscribe(EventType eventType, EventListener listener) {
        listeners.get(eventType).add(listener);
    }

    /**
     * 取消订阅
     *
     * @param eventType: 事件类型
     * @param listener:  监听
     * @return void
     * @author dralee
     * @date 2021/11/23 19:00
     **/
    public void unsubscribe(EventType eventType, EventListener listener) {
        listeners.get(eventType).remove(listener);
    }

    /**
     * 通知
     *
     * @param eventType 事件类型
     * @param result    结果
     */
    public void notify(EventType eventType, LotteryResult result) {
        for (EventListener listener : listeners.get(eventType)) {
            listener.notify(result);
        }
    }
}
